

import os 
import shutil


data_dir = "./data/raw_data/SegRap2023_Training_Set_120cases"
label_dir = "./SegRap2023_Training_Set_120cases_OneHot_Labels/Task001"
case_names = os.listdir(data_dir)

for case_name in case_names:

    if "segrap" not in case_name:
        continue

    if not os.path.exists(os.path.join(label_dir, f"{case_name}.nii.gz") ):
        continue

    need_to_save_dir = os.path.join(data_dir, case_name)
    shutil.move(os.path.join(label_dir, f"{case_name}.nii.gz"), os.path.join(need_to_save_dir, f"{case_name}.nii.gz"))

for case_name in case_names:
    if "segrap" not in case_name:
        continue

    need_to_save_dir = os.path.join(data_dir, case_name)
    for f in os.listdir(need_to_save_dir):
        ## 判断是否存在分割文件，如果存在，则重命名
        if f == f"{case_name}.nii.gz":
            shutil.move(os.path.join(need_to_save_dir, f"{case_name}.nii.gz"), os.path.join(need_to_save_dir, f"seg.nii.gz"))
